package controllers

import (
	"gin-worked/app/system/service"
	"gin-worked/app/system/validate"
	"gin-worked/work"
	"github.com/gin-gonic/gin"
)

type SystemController struct {
}

// Login  登录
func (s SystemController) Login(c *gin.Context) {
	var login validate.Login

	if err := c.ShouldBind(&login); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}

	param := c.MustGet("params").(map[string]string)
	username := param["Username"]
	password := param["Password"]
	res := service.Login(username, password)
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
}

// Info 获取后台用户详情
func (s SystemController) Info(c *gin.Context) {
	param := c.MustGet("params").(map[string]string)
	uid := param["id"]

	res := service.GetSystemInfo(uid)
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Add 添加后台管理人员
func (s SystemController) Add(c *gin.Context) {

	//fmt.Println(123)
	var add validate.SystemAdd
	if err := c.ShouldBind(&add); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	// 获取所有请求参数
	param := c.MustGet("params").(map[string]string)

	res := service.SystemAdd(param["Username"], param["Nickname"], param["Tel"], param["Headimg"], param["Password"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// List 获取管理人员列表信息
func (s SystemController) List(c *gin.Context) {

	param := c.MustGet("params").(map[string]string)
	offset := param["offset"]
	limit := param["limit"]
	res := service.SystemList(offset, limit)
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Edit 编辑后台管理人员信息
func (s SystemController) Edit(c *gin.Context) {
	var edit validate.SystemEdit
	if err := c.ShouldBind(&edit); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	// 获取所有请求参数
	param := c.MustGet("params").(map[string]string)
	res := service.SystemEdit(param["Id"], param["Nickname"], param["Tel"], param["Headimg"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// EditPwd 修改密码
func (s SystemController) EditPwd(c *gin.Context) {
	var editPwd validate.SystemEditPwd
	if err := c.ShouldBind(&editPwd); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.SystemEditPwd(param["Id"], param["Password"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// EditStatus 修改后台管理人员状态
func (s SystemController) EditStatus(c *gin.Context) {
	var editStatus validate.SystemEditStatus
	if err := c.ShouldBind(&editStatus); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.SystemEditStatus(param["Id"], param["Status"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// EditHeadImg 修改后台管理人员头像
func (s SystemController) EditHeadImg(c *gin.Context) {
	var editStatus validate.SystemEditHeadImg
	if err := c.ShouldBind(&editStatus); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}

	param := c.MustGet("params").(map[string]string)
	res := service.SystemEditHeadImg(param["Id"], param["Headimg"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}
